package com.cookbook.animals;

import android.app.*;
import android.os.*;
import android.view.View;
import android.widget.*;
import android.content.*;
import android.database.sqlite.*;
import org.apache.commons.*;


public class CreateDialog extends Dialog {
	// Declare variable to hold context
	Context myContext;
	
	// Declare variables to hold database helper and database
	DatabaseHelper db_helper;
	SQLiteDatabase db;
	
	// Declare EditText fields to hold user_name and password
	EditText user_name;
	EditText password;
	
	// Define constructor
	public CreateDialog(Context context){
		super(context);
		
		// Save given context in myContext
		myContext = context;
	}
	
	// Override onCreate function
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		// Set layout
		setContentView(R.layout.user);
		
		// Instantiate EditText fields
		user_name = (EditText) findViewById(R.id.user_name);
		password = (EditText) findViewById(R.id.password);
		
		// Check if savedInstanceState is non-null
		if(savedInstanceState != null){
			// If not then get previous text entered into user_name and password fields
				// and initialize that as text for both fields
			user_name.setText(savedInstanceState.getString("user_name"));
			password.setText(savedInstanceState.getString("password"));
		}
		
		// Set title
		setTitle("Create New Profile");
		
		// Instantiate two textviews to alter titles
		TextView new_user_title = (TextView) findViewById(R.id.user_name_title);
		TextView new_password_title = (TextView) findViewById(R.id.password_title);
		
		// Change text of both titles to reflect creating a new user
		new_user_title.setText("New Name");
		new_password_title.setText("New Password");
		
		// Instantiate save button
		Button save = (Button) findViewById(R.id.enter);
		
		// Change text on save to reflect creating a new user
		save.setText("Save New Profile");
		
		// Instantiate user_name and password
		user_name = (EditText) findViewById(R.id.user_name);
		
		// Set onClickListener for Button
		save.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Get text from both fields and save it to variables
				String name = user_name.getText().toString();
				String pass = password.getText().toString();
				
				// Trim both fields
				name = name.trim();
				pass = pass.trim();
				
				// Check if user_name is empty
				if(name.length() == 0){
					// If so give an error message
					Toast.makeText(myContext, "Enter a Name First", Toast.LENGTH_SHORT).show();
				}
				
				// Else check if password is empty
				else if(pass.length() == 0){
					// If so give an error message
					Toast.makeText(myContext, "Enter a Password First", Toast.LENGTH_SHORT).show();
				}
				
				// Else continue
				else{
					// Instantiate new DatabaseHelper variable
					db_helper = new DatabaseHelper(myContext);
					
					// Open database for writing and reading
					db = db_helper.getWritableDatabase();
					
					// Check if this username already exists
					if(db_helper.nameExists(db, name)){
						// If so give an error message
						Toast.makeText(myContext, "That name already exists", Toast.LENGTH_SHORT).show();
					
						// Close the database
						db.close();
					}
					
					// Else continue
					else{
						// Create new user in database
						db_helper.addUser(db, name, pass);

						// Close database
						db.close();

						// Create intent for Menu class
						Intent start_menu = new Intent(myContext, Menu.class);

						// Put user into start_menu as an extra
						start_menu.putExtra("user", name);

						// Start new activity using current context and start_menu intent
						myContext.startActivity(start_menu);

						// Upon returning to this activity close this dialog
						dismiss();
					}
				}
			}
		});
	}
	
    // Override onSavedInstanceState function
    @Override
    public void onRestoreInstanceState(Bundle outState){
    	// Call super function
    	super.onRestoreInstanceState(outState);
    	
    	// Get text from user_name and put it into outState
    	outState.putString("user_name", user_name.getText().toString());
    	
    	// Get text from password and put it into outState
    	outState.putString("password", password.getText().toString());
    }
}
